/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.UserBean;
import com.minierp.dao.UserDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class AccountFormCtr extends SimpleFormController {
	private UserDao userDao;

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		UserBean form = new UserBean();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		User u = (User) this.userDao.get(User.class, ub.getUser().getId());
		form.setUser(u);
		return form;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		UserBean form = (UserBean) command;
		User u = form.getUser();
		if (u.getPassword() == null || u.getPassword().equals("")) {
			u.setPassword(form.getCurrentPassword());
		}

		this.userDao.update(u);
		ub.getUser().setPassword(u.getPassword());
		ub.getUser().setUserName(u.getUserName());
		EncCommonHelper.setCurrentUserAccount(request, ub);
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String alertType = request.getParameter("alert_type");
		if (alertType != null && alertType.equals("SHOW")) {
			EncCommonHelper.setAlertMessage(request, "修改信息保存成功！");
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public UserDao getUserDao() {
		return this.userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}